var app = getApp();
var baseUrl = app.globalData.host;
Component({
  data: {
    formData: {
      oprAssetSituEachAssetName:[],
      familySituAssetName:[],
      extLiabSituEachLiabType:[],
    },
    
  },
  methods: {
    formInputChange(e) {
      let index = e.target.dataset.field
      let count = index.substring(index.length-2,index.length);
      let countName = index.substring(index.length-5,index.length-2);
      let name = index.substring(0,index.length-5)
      this.setData({
          [`formData.${name}`]: e.detail.value
      })
      let oprAssetSituEachAssetName = this.data.formData.oprAssetSituEachAssetName;
      let familySituAssetName = this.data.formData.familySituAssetName;
      let extLiabSituEachLiabType = this.data.formData.extLiabSituEachLiabType;
      let isAdd = true;
      if(countName === 'run')
      {
        if(e.detail.value !== ''){
          if(oprAssetSituEachAssetName.length === 0 )
          {
            oprAssetSituEachAssetName.push(count);
          }
          else{
            for(let i = 0; i<oprAssetSituEachAssetName.length ; i++){
              if(oprAssetSituEachAssetName[i] === count)
              {
                isAdd = false;
              }
            }
            if(isAdd)
            {
              oprAssetSituEachAssetName.push(count);
            }
          }
        }
        else{
          oprAssetSituEachAssetName.pop(count);
        }
      }
      else if(countName === 'hom'){
        if(e.detail.value !== ''){
          if(familySituAssetName.length === 0 )
          {
            familySituAssetName.push(count);
          }
          else{
            for(let i = 0; i<familySituAssetName.length ; i++){
              if(familySituAssetName[i] === count)
              {
                isAdd = false;
              }
            }
            if(isAdd)
            {
              familySituAssetName.push(count);
            }
          }
        }
        else{
          familySituAssetName.pop(count);
        }
      }
      else if(countName === 'for'){
        if(e.detail.value !== ''){
          if(extLiabSituEachLiabType.length === 0 )
          {
            extLiabSituEachLiabType.push(count);
          }
          else{
            for(let i = 0; i<extLiabSituEachLiabType.length ; i++){
              if(extLiabSituEachLiabType[i] === count)
              {
                isAdd = false;
              }
            }
            if(isAdd)
            {
              extLiabSituEachLiabType.push(count);
            }
          }
        }
        else{
          extLiabSituEachLiabType.pop(count);
        }
      }
      let oprAssetSituSumAmtValue = 0;
      let familyAssetSituSumAmtValue = 0;
      if(this.data.formData.invtyMktPrice != null && this.data.formData.invtyMktPrice > 0)
      {
        oprAssetSituSumAmtValue += parseInt(this.data.formData.invtyMktPrice)
      }
      if(this.data.formData.deviceMktPrice != null && this.data.formData.deviceMktPrice > 0)
      {
        oprAssetSituSumAmtValue += parseInt(this.data.formData.deviceMktPrice)
      }
      if(this.data.formData.rawMatMktPrice != null && this.data.formData.rawMatMktPrice>0)
      {
        oprAssetSituSumAmtValue += parseInt(this.data.formData.rawMatMktPrice)
      }
      if(this.data.formData.rcvablsMktPrice != null && this.data.formData.rcvablsMktPrice>0)
      {
        oprAssetSituSumAmtValue += parseInt(this.data.formData.rcvablsMktPrice)
      }
      if(this.data.formData.prePayMktPrice != null && this.data.formData.prePayMktPrice>0)
      {
        oprAssetSituSumAmtValue += parseInt(this.data.formData.prePayMktPrice)
      }
      if(this.data.formData.fixedAssetInputMktPrice != null && this.data.formData.fixedAssetInputMktPrice>0)
      {
        oprAssetSituSumAmtValue += parseInt(this.data.formData.fixedAssetInputMktPrice)
      }
      if(this.data.formData.cashMktPrice != null && this.data.formData.cashMktPrice>0)
      {
        oprAssetSituSumAmtValue += parseInt(this.data.formData.cashMktPrice)
      }
      if(this.data.formData.houseMktPrice != null && this.data.formData.houseMktPrice>0)
      {
        familyAssetSituSumAmtValue += parseInt(this.data.formData.houseMktPrice)
      }
      if(this.data.formData.carMktPrice != null && this.data.formData.carMktPrice>0)
      {
        familyAssetSituSumAmtValue += parseInt(this.data.formData.carMktPrice)
      }
      this.setData({
        [`formData.oprAssetSituSumAmt`] :oprAssetSituSumAmtValue,
        [`formData.familyAssetSituSumAmt`]:familyAssetSituSumAmtValue
      })
    },

    bindTextAreaBlur: function(e){
      this.setData({
          [`formData.canDispsNetAsetAnalyDesc`]:e.detail.value
      }) 
    },
    
    bindOtherAreaBlur:function(e){
      const {field} = e.currentTarget.dataset
      this.setData({
          [`formData.${field}`]: e.detail.value
      })
    },

    submitForm: function() {
      var data = this.data.formData;
      console.info(data);
      this.setData({
          disabled:true
      });
      wx.request({
        url: baseUrl+"/report/addRptFarmAssetInfoList",
        method: 'POST',
        dataType: 'json',
        data: data,
        header: {
          'content-type': 'application/json'
        },
        success: function(res) { 
          wx.showToast({
            title: "保存成功",
            icon: 'none',
            duration: 2000,
            success:function(){
              setTimeout(function () {
                //要延时执行的代码
                wx.navigateBack({ //跳转到前一个页面
                  delta:1,//前一个页面
                });
              }, 2000) //延迟时间
            }
          });
          },
          fail: function(res) {
              wx.showToast({
              title: '服务器网络错误,请稍后重试',
              icon: 'loading',
              duration: 1500
              })
          },
          complete: function(res) {
          },
      })
    },

    onLoad: function(option) {
      var that = this;
      that.setData({
        custId:option.custId,
          [`formData.custId`]: option.custId
      });
      var data = that.data.formData;  
      wx.request({
        url: baseUrl+"/report/selectRptFarmAssetInfo",
        method: 'POST',
        data: data,
        header: {
            'content-type': 'application/json'
        },
        dataType: 'json',
        success: function(res) { 
            console.info(res.data.code);
            if(res.data.code == 2)
            {
              wx.showToast({
                title: res.data.data,
                icon: 'none',
                duration: 2000,
                success:function(){
                  setTimeout(function () {
                    //要延时执行的代码
                    wx.navigateBack({ //跳转到前一个页面
                      delta:1,//前一个页面
                    });
                  }, 2000) //延迟时间
                }
              });
            }
            else
            {
              console.info(res.data.data);
              that.setData({
                formData: res.data.data,
              })
            }
        },
        fail: function(res) {
            wx.showToast({
            title: '服务器网络错误,请稍后重试',
            icon: 'loading',
            duration: 1500
            })
        },
        complete: function(res) {
        },
      })
    }
  }
});